package com.java.controller.admin;

import com.java.entity.User;
import com.java.service.UserService;
import com.java.util.PageUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/vue-admin-template/user")
public class AdminUserController {
    @Autowired
    UserService userService;

    @RequestMapping("/{page}/{limit}")
    public Map<String,Object> findAllUser(@PathVariable("page") String currentPage,@PathVariable("limit") String pageSize,String username){
        PageUtil<User> pageUtil = userService.selectAll(Integer.parseInt(currentPage.trim()),Integer.parseInt(pageSize.trim()) , username);
        System.out.println(pageUtil.getData());
        Map<String,Object> map=new HashMap<>();
        map.put("data",pageUtil);
        map.put("code",20000);
        if (pageUtil.getData().size()==0){
            map.put("message","没有数据");
            return map;
        }
        map.put("message","数据查询成功");
        return map;
    }
    @RequestMapping("/{userId}")
    public Map<String,Object> updatePermission(@PathVariable("userId") String userId,@Param("permission") Boolean permission){
        System.out.println(userId+"   "+permission);
        userService.updatePermission(userId, permission);
        Map<String,Object> map=new HashMap<>();
        map.put("data","");
        map.put("code",20000);
        map.put("message","用户登录权限修改成功");
        return map;
    }
    @RequestMapping("/update/{id}")
    public Map<String,Object> selectById(@PathVariable("id") String userId){
        User user = userService.selectById(userId);
        Map<String,Object> map=new HashMap<>();
        map.put("data",user);
        map.put("code",20000);
        map.put("message","查询用户个人信息成功");
        return map;
    }
    @RequestMapping("/addUser")
    public Map<String,Object> addUser(User user){
        userService.register(user);
        Map<String,Object> map=new HashMap<>();
        map.put("data","");
        map.put("code",20000);
        map.put("message","添加用户成功！");
        return map;
    }

    @RequestMapping("/updateUser")
    public Map<String,Object> updateUser(User user){
        userService.updateUsertoAdmin(user);
        Map<String,Object> map=new HashMap<>();
        map.put("data","");
        map.put("code",20000);
        map.put("message","添加用户成功！");
        return map;
    }
}
